package website.haicheng.cloudauth.controller;


import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import website.haicheng.cloudauth.dto.MenuDto;
import website.haicheng.cloudauth.service.IMenuService;
import website.haicheng.cloudauth.vo.MenuVo;
import website.haicheng.core.base.Resp;
import website.haicheng.core.util.JWTUtil;
import website.haicheng.core.validated.groups.InsertGroup;
import website.haicheng.core.validated.groups.UpdateGroup;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 582895699@qq.com
 * @since 2021-02-10
 */
@Api(tags = "菜单及权限接口")
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private IMenuService menuService;


    @ApiResponses(value = {@ApiResponse(code = 200, message = "成功", response = Resp.class), @ApiResponse(code = 500, message = "失败")})
    @ApiOperation(value = "添加参数及权限")
    @PostMapping(value = "/addMenu")
    public Resp addMenu(@Validated(value = InsertGroup.class) @RequestBody MenuDto dto, HttpServletRequest request) {
        menuService.addMenu(dto, JWTUtil.getAccountFromToken(request));
        return Resp.ok();
    }


    @ApiResponses(value = {@ApiResponse(code = 200, message = "成功", response = Resp.class), @ApiResponse(code = 500, message = "失败")})
    @ApiOperation(value = "修改参数及权限")
    @PostMapping(value = "/updateMenu")
    public Resp updateMenu(@Validated(value = UpdateGroup.class) @RequestBody MenuDto dto, HttpServletRequest request) {
        menuService.updateMenu(dto, JWTUtil.getAccountFromToken(request));
        return Resp.ok();
    }

    @ApiParam(name = "id")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "成功", response = Resp.class), @ApiResponse(code = 500, message = "失败")})
    @ApiOperation(value = "通过id逻辑删除菜单及权限")
    @DeleteMapping
    public Resp delMenu(@NotNull(message = "[id]不可为空") Long id, HttpServletRequest request) {
        menuService.delMenu(id, JWTUtil.getAccountFromToken(request));
        return Resp.ok();
    }

    @ApiResponses(value = {@ApiResponse(code = 200, message = "成功", response = MenuVo.class), @ApiResponse(code = 500, message = "失败")})
    @ApiOperation(value = "获取菜单、权限信息")
    @GetMapping(value = "/listMenus")
    public Resp listMenus() {
        List<MenuVo> menuVoList = menuService.listMenus();
        return Resp.ok(menuVoList);
    }
}
